package com.sims.aspect;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

import com.opensymphony.xwork2.ActionContext;

@Aspect
public class LoginAspect {
	// 配置切入点,该方法无方法体,主要为方便同类中其他方法使用此处配置的切入点
	@Pointcut("execution(* com.sims.action.LoginAction.login(..))")
	public void aspectLoin() {
	}
	@Pointcut("execution(* com.sims.action.LoginAction.logout(..))")
	public void aspectLogout() {
	}

	@Before("aspectLogout()")
	public void beforeAdvice() {
		String userName = "";
		if (ActionContext.getContext().getSession()
				.containsKey("currentUserAccount")) {
			userName = (String) ActionContext.getContext().getSession()
					.get("currentUserAccount");
		}
		if (userName != null && userName.length() > 0) {
			System.out.println("Before lotout username= " + userName);
		} else {
			System.out.println("Before lotout error");
		}
	}

	@After("aspectLoin()")
	public void afterAdvice() {
		String userName = "";
		if (ActionContext.getContext().getSession()
				.containsKey("currentUserAccount")) {
			userName = (String) ActionContext.getContext().getSession()
					.get("currentUserAccount");
		}
		if (userName != null && userName.length() > 0) {
			System.out.println("After login sucess username= " + userName);
		} else {
			System.out.println("After login error");
		}
	}
}
